package com.matrix.codeGeneration.ext;

import com.matrix.codeGeneration.convert.NameConvert;

/**
 * oracle风格的名称实现的转换器
 * 
 * @author Administrator
 *
 */
public class OracleStyleNameConvert implements NameConvert {

	@Override
	public String propertyToColumn(String property) {
		StringBuffer columnName = new StringBuffer(property);
		for (int i = 0; i < columnName.length(); i++) {
			if (columnName.charAt(i) >= 'A' && columnName.charAt(i) <= 'Z') {
				String upcase = "_" + (columnName.charAt(i) + "").toLowerCase();
				columnName.replace(i, i + 1, upcase);
			}
		}
		return columnName.toString();
	}

	/**
	 * 
	 */
	@Override
	public String columnToProperty(String column) {
		// 如果数据库字段为大写则使用这里的方法
		StringBuffer property = new  StringBuffer(tableNameToClassName(column.toLowerCase()));
		String tempF = (property.charAt(0) + "").toLowerCase();
		property.replace(0, 1, tempF);
		return property.toString();
	}

	@Override
	public String classNameToTableName(String calssName) {
		return propertyToColumn(calssName).substring(1, propertyToColumn(calssName).length());
	}

	/**
	 * 
	 * 首字母改为大写 去除_ 下划线后一个字符变为大写字符
	 */
	@Override
	public String tableNameToClassName(String tableName) {
		StringBuffer calssName = new StringBuffer(tableName.toLowerCase());
		String tempF = (calssName.charAt(0) + "").toUpperCase();
		calssName.replace(0, 1, tempF);
		for (int i = 0; i < calssName.length(); i++) {
			if (calssName.charAt(i) == '_' && i + 1 < tableName.length()) {
				String upcase = (calssName.charAt(i + 1) + "").toUpperCase();
				calssName.replace(i, i + 2, upcase);
			}
		}
		return calssName.toString();
	}

	/**
	 * 第一个字母大写
	 */
	@Override
	public String propertyToMethod(String property) {
		StringBuffer method = new StringBuffer(property);
		String tempF = (method.charAt(0) + "").toUpperCase();
		method.replace(0, 1, tempF);
		return method.toString();
	}

	/**
	 * 第一个字母小写
	 */
	@Override
	public String classNameToVariableName(String className) {
		StringBuffer variableName = new StringBuffer(className);
		String tempF = (variableName.charAt(0) + "").toLowerCase();
		variableName.replace(0, 1, tempF);
		return variableName.toString();
	}

	public static void main(String[] args) {
		OracleStyleNameConvert convert = new OracleStyleNameConvert();
		System.out.println(convert.columnToProperty("user_id"));
	}
}
